package com.graduation.controller;

import com.graduation.entity.User;
import com.graduation.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 校验用户是否注册成功
     *
     * @param user
     * @return
     */
    @PostMapping("/userReg.do")
    public String userRegister(User user, Model model) {
        Integer result = userService.userRegister(user);
        if (result == 1) {
            model.addAttribute("email", user.getEmail());
            model.addAttribute("password", user.getPassword());
            return "login-register";
        }
        return "404";
    }

    /**
     * 校验用户是否存在
     *
     * @return
     */
    @RequestMapping("/isExitsUser.ax")
    @ResponseBody
    public String isExitsUser(String email) {
        //ajax校验是否已经存在该用户
        Integer exitsUser = userService.isExitsUser(email);
        //返回代码
        if (exitsUser == 1) {
            return "exits";
        }
        return "notExits";
    }

    /**
     * 登入系统
     *
     * @param req
     * @param resp
     * @param email
     * @param password
     * @param state
     * @return
     */
    @PostMapping("/loginSys.do")
    public String doLoginSystem(HttpServletRequest req, HttpServletResponse resp, String email, String password, String state) {
        HttpSession session = req.getSession();
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setState(state);
        session.setAttribute("user", user);
        Integer integer = userService.loginSystem(req, resp, email, password, state);
        if (integer == 1) {
            return "index";
        }
        return "login-register";
    }

    /**
     * @param email
     * @return 返回json格式的用户数据
     */
    @PostMapping("/findInfoById.do")
    @ResponseBody
    public User findUserById(String email) {
        return userService.findUserById(email);
    }

    /**
     * 更新用户数据
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUserInfo.do")
    public String updateUserBaseInfo(User user) {
        Integer result = userService.updateUserBaseInfo(user);
        if (result == 1) {
            return "index";
        }
        return "my-account";
    }

    @GetMapping("/logOut.do")
    public String userLogOut(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        session.removeAttribute("user");
        return "index";
    }
}
